package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.DriverWorkInfo;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.DriverWorkInfoRepository;
import co.yixiang.modules.shop.service.DriverWorkInfoService;
import co.yixiang.modules.shop.service.dto.DriverWorkInfoDto;
import co.yixiang.modules.shop.service.dto.DriverWorkInfoQueryCriteria;
import co.yixiang.modules.shop.service.mapper.DriverWorkInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "driverWorkInfo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DriverWorkInfoServiceImpl implements DriverWorkInfoService {

    private final DriverWorkInfoRepository driverWorkInfoRepository;

    private final DriverWorkInfoMapper driverWorkInfoMapper;

    public DriverWorkInfoServiceImpl(DriverWorkInfoRepository driverWorkInfoRepository, DriverWorkInfoMapper driverWorkInfoMapper) {
        this.driverWorkInfoRepository = driverWorkInfoRepository;
        this.driverWorkInfoMapper = driverWorkInfoMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(DriverWorkInfoQueryCriteria criteria, Pageable pageable){
        Page<DriverWorkInfo> page = driverWorkInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(driverWorkInfoMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<DriverWorkInfoDto> queryAll(DriverWorkInfoQueryCriteria criteria){
        return driverWorkInfoMapper.toDto(driverWorkInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public DriverWorkInfoDto findById(Integer id) {
        DriverWorkInfo driverWorkInfo = driverWorkInfoRepository.findById(id).orElseGet(DriverWorkInfo::new);
        ValidationUtil.isNull(driverWorkInfo.getId(),"DriverWorkInfo","id",id);
        return driverWorkInfoMapper.toDto(driverWorkInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public DriverWorkInfoDto create(DriverWorkInfo resources) {
        return driverWorkInfoMapper.toDto(driverWorkInfoRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(DriverWorkInfo resources) {
        DriverWorkInfo driverWorkInfo = driverWorkInfoRepository.findById(resources.getId()).orElseGet(DriverWorkInfo::new);
        ValidationUtil.isNull( driverWorkInfo.getId(),"DriverWorkInfo","id",resources.getId());
        driverWorkInfo.copy(resources);
        driverWorkInfoRepository.save(driverWorkInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            driverWorkInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DriverWorkInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DriverWorkInfoDto driverWorkInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", driverWorkInfo.getUserId());
            map.put("标题", driverWorkInfo.getTitle());
            map.put("手机号", driverWorkInfo.getPhone());
            map.put("年限", driverWorkInfo.getYears());
            map.put("省", driverWorkInfo.getProvince());
            map.put("市", driverWorkInfo.getCity());
            map.put("区", driverWorkInfo.getDistrict());
            map.put("薪资下限", driverWorkInfo.getSalaryMin());
            map.put("薪资上限", driverWorkInfo.getSalaryMax());
            map.put("具体内容", driverWorkInfo.getMessage());
            map.put("图片", driverWorkInfo.getImage());
            map.put("阅读次数", driverWorkInfo.getCountNumber());
            map.put("创建时间", driverWorkInfo.getCreateTime());
            map.put("是否删除", driverWorkInfo.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}